<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "gky_user".
 *
 * @property integer $id
 * @property string $username
 * @property string $email
 * @property string $password_hash
 * @property string $auth_key
 * @property integer $confirmed_at
 * @property string $unconfirmed_email
 * @property integer $blocked_at
 * @property string $registration_ip
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $flags
 * @property integer $parentserverid
 *
 * @property GkyProfile $gkyProfile
 * @property Dept $dept
 * @property GkyToken[] $gkyTokens
 * @property SocialAccount[] $socialAccounts
 */
class GkyUser extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'email', 'password_hash', 'auth_key', 'created_at', 'updated_at'], 'required'],
            [['confirmed_at', 'blocked_at', 'created_at', 'updated_at', 'flags', 'parentserverid'], 'integer'],
            [['username', 'email', 'unconfirmed_email'], 'string', 'max' => 255],
            [['password_hash'], 'string', 'max' => 60],
            [['auth_key'], 'string', 'max' => 32],
            [['registration_ip'], 'string', 'max' => 45],
            [['email'], 'unique'],
            [['username'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => '用户名',
            'email' => '邮件',
            'password_hash' => '用户密码',
            'auth_key' => 'Auth Key',
            'confirmed_at' => 'Confirmed At',
            'unconfirmed_email' => 'Unconfirmed Email',
            'blocked_at' => 'Blocked At',
            'registration_ip' => '注册的ip',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'flags' => 'Flags',
            'parentserverid' => '父级服务结点',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGkyProfile()
    {
        return $this->hasOne(GkyProfile::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGkyTokens()
    {
        return $this->hasMany(GkyToken::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSocialAccounts()
    {
        return $this->hasMany(SocialAccount::className(), ['user_id' => 'id']);
    }

    public static function findInfo($userid){
        $user = GkyUser::findOne($userid);
        $result = [];
        $result['username']=$user->username;
        $result['email']=$user->email;
        $result['realname']=isset($user->gkyProfile) ? $user->gkyProfile->name : '未设置';
        $result['weixinid']=isset($user->gkyProfile) ? $user->gkyProfile->weixinid : '未设置';
        $result['dname']=isset($user->gkyProfile->dept) ? $user->gkyProfile->dept->dname : '未设置';
        $result['bio']=isset($user->gkyProfile) ? $user->gkyProfile->bio : '未设置';
        return $result;
    }
}
